/*
 * UtilTests.cpp
 *
 *  Created on: Mar 2, 2011
 *      Author: Craig Wilson
 */
#include <iostream>
#include <stdlib.h>
#include "../src/URLDownloader.cpp"
#include "../src/HTMLParser.cpp"
#include "../src/StopWordList.cpp"
#include "../src/XMLGenerator.cpp"

	using namespace std;

	extern PageHistory *history;
	extern Page *p1,*p2,*p3;
	extern void iniValues();
	extern KeyWordIndex *Index;

	int XMLGeneratorTests(){

		try{

			XMLGenerator xml;
			iniValues();

			string words[4]= {"hello", "test","avacado","horse"};
			history->add(*p1);
			history->add(*p2);
			history->add(*p3);
			Index->add(words[0],*p1);
			Index->add(words[0],*p1);
			Index->add(words[1],*p1);
			Index->add(words[2],*p1);
			Index->add(words[2],*p1);
			Index->add(words[2],*p1);
			Index->add(words[1],*p2);
			Index->add(words[1],*p2);
			Index->add(words[0],*p2);
			Index->add(words[0],*p2);
			Index->add(words[3],*p3);
			Index->add(words[3],*p3);
			Index->add(words[3],*p3);
			Index->add(words[3],*p3);

			char file[]="output.txt";
			xml.generate(file,*history,*Index);

		}
		catch(...){
			return 1; //Unhandled Exception
		}
		return 0;
	}

	int URLDownloaderTests(){
		try{
			string test("http://students.cs.byu.edu/~cs240ta/winter2011/");
			URL site(&test);
			URLDownloader download;
			string *HTML = download.download(&site);
		}
		catch(...){
			cout << "Unhandled Exception" <<endl;
		}
		return 0;
	}


	int HTMLParserTests(){
		ifstream file;

		file.open("SampleHTML.txt");
		string text("");
		while(!file.eof()){
			char temp[2048];
			file.getline(temp,2048);
			text+=temp;
			text+="\n";
		}

		try {
			HTMLParser parse;

			parse.getDescription(&text);
			parse.getLinks(&text);
			parse.getWords(&text);
		}
		catch(...){
			return 1; //Something broke
		}
		return 0;
	}


	int StopWordListTests(){
		try{
			char file[]= "stopword.txt";
			StopWordList stop(file);
			string a="a";
			string b="Aardvark";
			string c="WenTWorth";

			if (!stop.contains(&a) || stop.contains(&b) || !stop.contains(&c)){
				return 1; //Contains() incorrect
			}
		}
		catch(...){
			return 2;	//Unhandeled Exception
		}
		return 0;
	}
